package com.airbat.prize.modules.system.dal.mysql.user;

import com.airbat.prize.common.pojo.PageResult;
import com.airbat.prize.modules.system.controller.user.vo.user.SysUserExportReqVO;
import com.airbat.prize.modules.system.controller.user.vo.user.SysUserPageReqVO;
import com.airbat.prize.modules.system.dal.dataobject.user.SysUserDO;
import com.airbat.prize.mybits.mybatis.core.mapper.BaseMapperX;
import com.airbat.prize.mybits.mybatis.core.query.QueryWrapperX;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.ibatis.annotations.Mapper;

import java.util.Collection;
import java.util.List;

@Mapper
public interface SysUserMapper extends BaseMapperX<SysUserDO> {

    default SysUserDO selectByUsername(String username) {
        return selectOne(new QueryWrapper<SysUserDO>().eq("username", username).or().eq("police_no", username));
    }

    default SysUserDO selectByIdcard(String idcard) {
        return selectOne(new QueryWrapper<SysUserDO>().eq("idcard", idcard));
    }

    default SysUserDO selectByMobile(String mobile) {
        return selectOne(new QueryWrapper<SysUserDO>().eq("mobile", mobile));
    }

    default SysUserDO selectByPoliceNo(String policeNo) {
        return selectOne(new QueryWrapper<SysUserDO>().eq("police_no", policeNo));
    }

    default SysUserDO selectByEmail(String email) {
        return selectOne(new QueryWrapper<SysUserDO>().eq("email", email));
    }

    default PageResult<SysUserDO> selectPage(SysUserPageReqVO reqVO, Collection<String> structureIds) {
        return selectPage(reqVO, new QueryWrapperX<SysUserDO>()
                .likeIfPresent("username", reqVO.getUsername())
                .likeIfPresent("nickname", reqVO.getNickname())
                .likeIfPresent("mobile", reqVO.getMobile())
                .likeIfPresent("idcard", reqVO.getIdcard())
                .likeIfPresent("mobile_short", reqVO.getMobileShort())
                .likeIfPresent("police_no", reqVO.getPoliceNo())
                .eqIfPresent("dept_Id", reqVO.getDeptId())
                .eqIfPresent("status", reqVO.getStatus())
                .betweenIfPresent("create_time", reqVO.getBeginTime(), reqVO.getEndTime())
                .inIfPresent("structure_id", structureIds)
                .orderByDesc("create_time", "login_date"));
    }

    default List<SysUserDO> selectList(SysUserExportReqVO reqVO, Collection<Long> deptIds) {
        return selectList(new QueryWrapperX<SysUserDO>().likeIfPresent("username", reqVO.getUsername())
                .likeIfPresent("mobile", reqVO.getMobile())
                .eqIfPresent("status", reqVO.getStatus())
                .betweenIfPresent("create_time", reqVO.getBeginTime(), reqVO.getEndTime())
                .inIfPresent("dept_id", deptIds));
    }

    default List<SysUserDO> selectListByNickname(String nickname) {
        return selectList(new QueryWrapperX<SysUserDO>().like("nickname", nickname));
    }

    default List<SysUserDO> selectListByUsername(String username) {
        return selectList(new QueryWrapperX<SysUserDO>().like("username", username));
    }

}

